//
//  TalkCell.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/7/1.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

class TalkCell: TableRowView {
    
    var info:ChatData?
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
    }
    
    convenience init(info:ChatData,identifier:String) {
        self.init()
        self.info = info
        self.identifier = NSUserInterfaceItemIdentifier.init(identifier)
        self.setupUI()
        self.loadData()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func loadData()  {
        guard let uInfo = self.info else {
            return
        }
        
        self.avatorImg.text = uInfo.formName
        self.fromLab.stringValue = uInfo.formName
        let toUsr = uInfo.toNames.count==0 ? "所有人":uInfo.toNames[0]
        self.toLab.stringValue = toUsr
        self.contentLab.stringValue = uInfo.text
        self.timeLab.stringValue = uInfo.time
    }
    
    func setupUI()  {
        self.wantsLayer = true
        //self.backgroundColor = color_random_rgba()
        
        self.addSubview(baisc)
        baisc.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(10)
            make.height.equalTo(30)
            make.centerX.width.equalToSuperview()
        }
        baisc.addSubview(timeLab)
        timeLab.snp.makeConstraints { (make) in
            make.right.equalTo(-10)
            make.height.equalTo(16)
            make.width.equalTo(40)
            make.centerY.equalToSuperview()
        }
        
        baisc.addSubview(avatorImg)
        avatorImg.snp.makeConstraints { (make) in
            make.width.height.equalTo(30)
            make.centerY.equalToSuperview()
            make.left.equalTo(10)
        }
        
        baisc.addSubview(fromLab)
        fromLab.snp.makeConstraints { (make) in
            make.height.equalTo(16)
            make.centerY.equalToSuperview()
            make.left.equalTo(avatorImg.snp.right).offset(0)
        }
        
        baisc.addSubview(formToLab)
        formToLab.snp.makeConstraints { (make) in
            make.height.equalTo(16)
            make.centerY.equalToSuperview()
            make.left.equalTo(fromLab.snp.right).offset(0)
        }
        baisc.addSubview(toLab)
        toLab.snp.makeConstraints { (make) in
            make.height.equalTo(16)
            make.centerY.equalToSuperview()
            make.left.equalTo(formToLab.snp.right).offset(2)
        }
        
        let height = self.info?.text.ga_height(font: 12, width: kTRTCUserWidth-60) ?? 20
        self.addSubview(contentLab)
        contentLab.snp.makeConstraints { (make) in
            make.height.equalTo(height)
            make.width.equalTo(kTRTCUserWidth-60)
            make.centerX.equalToSuperview()
            make.top.equalTo(baisc.snp.bottom).offset(10)
            
        }
    }
    
    lazy var baisc: NSView = {
        let view = NSView()
        return view
    }()
    
    lazy var avatorImg: KImageView = {
        let imageV = KImageView.init()
        imageV.wantsLayer = true
        imageV.layer?.backgroundColor = color_random_rgba().cgColor
        imageV.layer?.cornerRadius = 6
        imageV.layer?.masksToBounds = true
        imageV.text = "name"
        return imageV
    }()
    
    lazy var fromLab: NSLabel = {
        let lab = NSLabel.label(title: "zhangheng to", textColor: color_gray(100), font: NSFont.systemFont(ofSize: 12))
        lab.sizeToFit()
        return lab
    }()
    
    lazy var formToLab: NSLabel = {
        let lab = NSLabel.label(title: "to", textColor: color_gray(100), font: NSFont.systemFont(ofSize: 12))
        lab.sizeToFit()
        return lab
    }()
    
    lazy var toLab: NSLabel = {
        let lab = NSLabel.label(title: "刘哲君:", textColor: color_main140, font: NSFont.systemFont(ofSize: 12))
        lab.sizeToFit()
        return lab
    }()
    
    lazy var contentLab: NSLabel = {
        let lab = NSLabel.label(title: "你好帅哥们都说了放辣椒了", textColor: color_gray(230), font: NSFont.systemFont(ofSize: 12))
        lab.maximumNumberOfLines = 0
        lab.lineBreakMode = .byCharWrapping
        lab.preferredMaxLayoutWidth = kTRTCUserWidth-60
        return lab
    }()
    lazy var timeLab: NSLabel = {
        let lab = NSLabel.label(title: "17:35", textColor: color_gray(160), font: NSFont.systemFont(ofSize: 12))
        lab.alignment = .right
        return lab
    }()
    
}
